package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.entity.Asks;
import com.github.pagehelper.Page;
import com.service.AsksService;
import com.util.VeDate;

@RestController //定义为控制器 返回JSON类型数据
@RequestMapping(value = "/asks", produces = "application/json; charset=utf-8")// 设置请求路径
@CrossOrigin // 允许跨域访问其资源
public class AsksController { // extends BaseController {
	// TODO Auto-generated method stub

	@Autowired // @Autowired的作用是自动注入依赖的ServiceBean
	private AsksService asksService;

	// 预处理 获取基础参数
	@GetMapping(value = "createAsks.action")
	public Map<String, Object> createAsks() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("today", VeDate.getStringDateShort());
		return map;
	}

	// 新增提示问题
	@PostMapping(value = "insertAsks.action")
	public Map<String, Object> insertAsks(@RequestBody String jsonStr) {
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
		Asks asks = new Asks();
		asks.setQuestions(obj.getString("questions")); //  为问题赋值
		asks.setAddtime(VeDate.getStringDateShort()); // 为创建日期赋值 
		asks.setMemo(obj.getString("memo")); //  为备注赋值
		int num = this.asksService.insertAsks(asks);
		if (num > 0) {
			map.put("success", false);
			map.put("code", num);
			map.put("message", "保存成功");
		} else {
			map.put("success", false);
			map.put("code", num);
			map.put("message", "保存失败");
		}
		return map;
	}

	// 按主键删除一个提示问题
	@GetMapping(value = "deleteAsks.action")
	public Map<String, Object> deleteAsks(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		int num = this.asksService.deleteAsks(id);
		if (num > 0) {
			map.put("success", true);
			map.put("code", num);
			map.put("message", "删除成功");
		} else {
			map.put("success", false);
			map.put("code", num);
			map.put("message", "删除失败");
		}
		return map;
	}

	// 按主键批量删除提示问题
	@PostMapping(value = "deleteAsksByIds.action")
	public Map<String, Object> deleteAsksByIds(@RequestBody String[] ids) {
		int num = 0;
		for (String asksid : ids) {
			num += this.asksService.deleteAsks(asksid);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if (num > 0) {
			map.put("success", true);
			map.put("code", num);
			map.put("message", "删除成功");
		} else {
			map.put("success", false);
			map.put("code", num);
			map.put("message", "删除失败");
		}
		return map;
	}

	// 修改提示问题
	@PostMapping(value = "updateAsks.action")
	public Map<String, Object> updateAsks(@RequestBody String jsonStr) {
		JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
		Asks asks = this.asksService.getAsksById(obj.getString("asksid")); // 获取object中asksid字段
		asks.setQuestions(obj.getString("questions")); //  为问题赋值
		asks.setMemo(obj.getString("memo")); //  为备注赋值

		Map<String, Object> map = new HashMap<String, Object>();
		int num = this.asksService.updateAsks(asks);
		if (num > 0) {
			map.put("success", true);
			map.put("code", num);
			map.put("message", "修改成功");
		} else {
			map.put("success", false);
			map.put("code", num);
			map.put("message", "修改失败");
		}
		return map;
	}

	// 查询全部提示问题数据 在下拉菜单中显示
	@GetMapping(value = "getAllAsks.action")
	public List<Asks> getAllAsks() {
		return this.asksService.getAllAsks();
	}

	// 按关键字查询提示问题数据 在下拉菜单中显示
	@GetMapping(value = "getAsksMap.action")
	public Map<String, Object> getAsksMap(String keywords) {
		Map<String, Object> map = new HashMap<String, Object>();
		Asks asks = new Asks();
		asks.setQuestions(keywords);
		List<Asks> list = this.asksService.getAsksByLike(asks);
		map.put("data", list);
		return map;
	}

	// 通过AJAX在表格中显示提示问题数据
	@GetMapping(value = "getAsksByPage.action")
	public Map<String, Object> getAsksByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
		// 定义一个Map对象 用来返回数据
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Asks> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
		List<Asks> list = this.asksService.getAllAsks();
		// 返回的map中定义数据格式
		map.put("count", pager.getTotal());
		map.put("total", list.size());
		map.put("data", list);
		map.put("code", 0);
		map.put("msg", "");
		map.put("page", page);
		map.put("limit", limit);
		return map;
	}

	// 通过AJAX在表格中显示提示问题数据
	@GetMapping(value = "getAsks.action")
	public Map<String, Object> getAsks(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String keywords) {
		// 定义一个Map对象 用来返回数据
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Asks> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
		Asks asks = new Asks();
		asks.setQuestions(keywords);
		List<Asks> list = this.asksService.getAsksByLike(asks);
		// 返回的map中定义数据格式
		map.put("count", pager.getTotal());
		map.put("total", list.size());
		map.put("data", list);
		map.put("code", 0);
		map.put("msg", "");
		map.put("page", page);
		map.put("limit", limit);
		return map;
	}

	// 通过AJAX在表格中显示提示问题数据
	@GetMapping(value = "getOwnerAsks.action")
	public Map<String, Object> getOwnerAsks(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit, String id) {
		// 定义一个Map对象 用来返回数据
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Asks> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
		Asks asks = new Asks();
		//asks.setAdminid(id);
		List<Asks> list = this.asksService.getAsksByLike(asks);
		// 返回的map中定义数据格式
		map.put("count", pager.getTotal());
		map.put("total", list.size());
		map.put("data", list);
		map.put("code", 0);
		map.put("msg", "");
		map.put("page", page);
		map.put("limit", limit);
		return map;
	}

	// 按主键查询提示问题数据
	@GetMapping(value = "getAsksById.action")
	public Asks getAsksById(String id) {
		Asks asks = this.asksService.getAsksById(id);
		return asks;
	}

	// TODO Auto-generated method stub
}
//


